package vip.xiaonuo.vein.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.*;
import vip.xiaonuo.facedoor.vo.haiqing.*;
import vip.xiaonuo.vein.controller.vo.AddOrUpdateUser;
import vip.xiaonuo.vein.controller.vo.DeleteUser;
import vip.xiaonuo.vein.model.ReqUtil;
import vip.xiaonuo.vein.modular.veindeviceevent.entity.VeinDeviceEvent;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventPersonTypeEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventStatusEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventSysTypeEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.service.VeinDeviceEventService;
import vip.xiaonuo.vein.websocket.WebSocket;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service(value = RuleConstant.VEIN + RuleConstant.FACEDOOR_DTO)
public class VeinFaceDoorDtoServiceImpl implements IFaceDoorDtoService {

    @Resource
    private WebSocket webSocket;

    @Autowired
    private DeviceLogService logService;

    @Autowired
    private DeviceBaseService deviceBaseService;

    @Autowired
    private ReqUtil reqUtil;

    @Autowired
    private VeinDeviceEventService veinDeviceEventService;

    @Override
    public DeviceStateResponse getDeviceState(String deviceId) throws Exception {
        return null;
    }

    @Override
    public ReturnJson deletePerson(String deviceId, DeletePersonRequest deletePersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在！");
        }
        DeleteUser deleteUser = new DeleteUser();
        List<String> userIds = new ArrayList<>();
        userIds.add(deletePersonRequest.getPersonnelId());
        deleteUser.setDeleteUserInfo(userIds);
        String reqSeriNo = reqUtil.getReqSeriNo(deleteUser);
        String req = reqSeriNo.concat(JSON.toJSONString(deleteUser));
        VeinDeviceEvent veinDeviceEvent = new VeinDeviceEvent()
                .setBody(deletePersonRequest.getMemberOrVisitorDeviceId().toString())
                .setRequest(req)
                .setStatus(VeinDeviceEventStatusEnum.EXECUTE.getCode())
                .setSerialNumber(reqSeriNo)
                .setDeviceId(deviceBase.getId());
        veinDeviceEvent.setSysType(VeinDeviceEventSysTypeEnum.MEMBER.getCode());
        veinDeviceEvent.setType(VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode());
        veinDeviceEventService.save(veinDeviceEvent);



//        logService.addDeviceLog(deletePersonRequest.getMemberName(), deviceBase.getName(), req, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode(), reqSeriNo);
        webSocket.sendOneMessage(deviceBase.getRegistrationCode(), req);
        return null;
    }

    @Override
    public ReturnJson deleteAllPerson(String uri, DeletePersonRequest deletePersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deletePersonRequest.getDeviceId());
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在！");
        }
        DeleteUser deleteUser = new DeleteUser();
        List<String> userIds = deletePersonRequest.getPersonnelIds().stream().map(Objects::toString).map("1"::concat).collect(Collectors.toList());
        deleteUser.setDeleteUserInfo(userIds);
        String reqSeriNo = reqUtil.getReqSeriNo(deleteUser);
        String req = reqSeriNo.concat(JSON.toJSONString(deleteUser));
        VeinDeviceEvent veinDeviceEvent = new VeinDeviceEvent()
                .setRequest(req)
                .setBody(String.join(",", userIds))
                .setStatus(VeinDeviceEventStatusEnum.EXECUTE.getCode())
                .setSerialNumber(reqSeriNo)
                .setDeviceId(deviceBase.getId())
                .setSysType(VeinDeviceEventSysTypeEnum.MEMBER.getCode())
                .setType(VeinDeviceEventPersonTypeEnum.DELETE_BATCH_MEMBER.getCode());
        veinDeviceEventService.save(veinDeviceEvent);
//        logService.addDeviceLog(deletePersonRequest.getMemberName(), deviceBase.getName(), req, VeinDeviceEventPersonTypeEnum.DELETE_PERSON.getCode(), reqSeriNo);
        webSocket.sendOneMessage(deviceBase.getRegistrationCode(), req);
        return null;
    }


    @Override
    public ReturnJson addPerson(String deviceId, AddPersonRequest addPersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在！");
        }
        AddOrUpdateUser addOrUpdateUser = new AddOrUpdateUser();
        AddOrUpdateUser.AddUserInfoDTO addUserInfoDTO = new AddOrUpdateUser.AddUserInfoDTO();
        addUserInfoDTO.setId(addPersonRequest.getJobNum());
        addUserInfoDTO.setName(addPersonRequest.getName());
        addUserInfoDTO.setUserUuid(addPersonRequest.getPersonnelId());
        addOrUpdateUser.setAddUserInfo(CollectionUtil.newArrayList(addUserInfoDTO));
        String reqSeriNo = reqUtil.getReqSeriNo(addOrUpdateUser);
        String req = reqSeriNo.concat(JSON.toJSONString(addOrUpdateUser));
        VeinDeviceEvent veinDeviceEvent = new VeinDeviceEvent()
                .setBody(addPersonRequest.getMemberOrVisitorDeviceId().toString())
                .setRequest(req)
                .setStatus(VeinDeviceEventStatusEnum.EXECUTE.getCode())
                .setSerialNumber(reqSeriNo)
                .setDeviceId(deviceBase.getId())
                .setSysType(VeinDeviceEventSysTypeEnum.MEMBER.getCode())
                .setType(VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode());
        veinDeviceEventService.save(veinDeviceEvent);
//        logService.addDeviceLog(addPersonRequest.getName(), deviceBase.getName(), req, VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode(), reqSeriNo);
        webSocket.sendOneMessage(deviceBase.getRegistrationCode(), req);
        return null;
    }


    @Override
    public ReturnJson updatePerson(String deviceId, UpdatePersonRequest updatePersonRequest) throws Exception {
        AddOrUpdateUser addOrUpdateUser = new AddOrUpdateUser();
        AddOrUpdateUser.AddUserInfoDTO addUserInfoDTO = new AddOrUpdateUser.AddUserInfoDTO();
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在！");
        }
        addUserInfoDTO.setId(updatePersonRequest.getJobNum());
        addUserInfoDTO.setName(updatePersonRequest.getName());
        addUserInfoDTO.setUserUuid(updatePersonRequest.getPersonnelId());
        addOrUpdateUser.setAddUserInfo(CollectionUtil.newArrayList(addUserInfoDTO));
        String reqSeriNo = reqUtil.getReqSeriNo(addOrUpdateUser);
        String req = reqSeriNo.concat(JSON.toJSONString(addOrUpdateUser));
        VeinDeviceEvent veinDeviceEvent = new VeinDeviceEvent()
                .setBody(updatePersonRequest.getMemberOrVisitorDeviceId().toString())
                .setRequest(req)
                .setSysType(VeinDeviceEventSysTypeEnum.MEMBER.getCode())
                .setStatus(VeinDeviceEventStatusEnum.EXECUTE.getCode())
                .setSerialNumber(reqSeriNo)
                .setDeviceId(deviceBase.getId())
                .setType(VeinDeviceEventPersonTypeEnum.ADD_PERSON.getCode())
                .setSysType(VeinDeviceEventSysTypeEnum.MEMBER.getCode())
                .setType(VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode());
        veinDeviceEventService.save(veinDeviceEvent);
        webSocket.sendOneMessage(deviceBase.getRegistrationCode(), req);
//        logService.addDeviceLog(updatePersonRequest.getName(), deviceBase.getName(), req, VeinDeviceEventPersonTypeEnum.UPDATE_PERSON.getCode(), reqSeriNo);
        return null;
    }

    @Override
    public GetPersonResponse getPerson(String uri, GetPersonRequest getPersonRequest) throws Exception {
        throw new ServiceException(500, "该版本暂时不支持此操作");

    }

    @Override
    public GetPersonResponse listPerson(String uri, GetPersonRequest request) throws Exception {
        throw new ServiceException(500, "该版本暂时不支持此操作");
    }

    @Override
    public BaseResponse setUserPwd(String uri, SetUserPwdRequest setUserPwdRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setFactoryDefault(String uri, SetFactoryDefaultRequest setFactoryDefaultRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSysTime(String uri, SetSysTimeRequest setSysTimeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse openDoor(String uri, OpenDoorRequest openDoorRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetGeneralParamRequest getGeneralParam(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setGeneralParam(String uri, GetAndSetGeneralParamRequest getAndSetGeneralParamRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse rebootDevice(String uri, RebootDeviceRequest rebootDeviceRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetDoorCondition getDoorCondition(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setDoorCondition(String uri, GetAndSetDoorCondition getAndSetDoorCondition, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetSoundRequest getSound(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSound(String uri, GetAndSetSoundRequest getAndSetSoundRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSubscribe(String uri, SetSubscribeRequest setSubscribeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }
}
